/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: ListenerPriorityBufferTestUsingJMock.java 420 2007-02-11 08:20:33Z jack.rybicki $
 */
package logmatcher.buffers;

import logmatcher.InputFilter;
import logmatcher.LogEventListener;

import org.jmock.Mock;
import org.jmock.MockObjectTestCase;

import org.jmock.expectation.ReturnValue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;


/**
 * DOCUMENT ME!
 *
 * @version $Rev: 420 $
*/
public class ListenerPriorityBufferTestUsingJMock extends MockObjectTestCase {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** TODO: DOCUMENT ME! */
    ListenerPriorityBuffer buffer;

    /** TODO: DOCUMENT ME! */
    Mock acceptInputFilter;

    /** TODO: DOCUMENT ME! */
    Mock rejectInputFilter;

    /** TODO: DOCUMENT ME! */
    Mock mockMessageListener;

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * DOCUMENT ME!
     *
     * @throws Exception TODO: DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Before
    public void setUp() throws Exception {
        buffer = new ListenerPriorityBuffer();
        acceptInputFilter = mock(InputFilter.class);
        rejectInputFilter = mock(InputFilter.class);
        mockMessageListener = mock(LogEventListener.class);
    }


    /**
     * DOCUMENT ME!
     *
     * @throws Exception TODO: DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @After
    public void tearDown() throws Exception {
    }


    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Test
    public void testUnlimittedBuffer() {
        buffer = new ListenerPriorityBuffer();
        assertNotNull(buffer);
    }


    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Test
    public void testAddMessageListener2() {
        buffer.addLogEventListener(
            (LogEventListener)mockMessageListener.proxy(), (InputFilter)acceptInputFilter.proxy());
    }


    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Test
    public void testAddMessageListener1() {
        buffer.addLogEventListener((LogEventListener)mockMessageListener.proxy());
    }


    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Test
    public void testAccepted() {
        //first install subscription
        buffer.addLogEventListener(
            (LogEventListener)mockMessageListener.proxy(), (InputFilter)acceptInputFilter.proxy());

        Object message = new Object();

        ReturnValue rv = new ReturnValue("true");
        rv.setValue(true);

        //expectations
        acceptInputFilter.expects(once()).method("accept").with(eq(message)).will(returnValue(true));
        mockMessageListener.expects(once()).method("receiveLogEvent").with(eq(message));

        buffer.receiveLogEvent(message);
    }


    /**
     * DOCUMENT ME!
     *
     * @todo DOCUMENT ME!
     */
    @Test
    public void testRejected() {
        //first install subscription
        buffer.addLogEventListener(
            (LogEventListener)mockMessageListener.proxy(), (InputFilter)acceptInputFilter.proxy());

        Object message = new Object();

        ReturnValue rv = new ReturnValue("true");
        rv.setValue(true);

        //expectations
        acceptInputFilter.expects(once()).method("accept").with(eq(message)).will(returnValue(false));
        mockMessageListener.expects(never()).method("receiveLogEvent");

        buffer.receiveLogEvent(message);
    }
}
